
package com.maven.front.controller.frontSubject;

import com.maven.front.aop.MyLog;
import com.maven.front.bean.EduComment;
import com.maven.front.bean.Result;
import com.maven.front.dto.EduCommentDto;
import com.maven.front.service.frontSubject.SubjectCommentService;
import com.maven.front.service.frontSubject.SubjectService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

//import com.maven.front.service.frontSubject.SubjectCommentService;


/**
 * @program: web-front
 * @description: 前台评论、点赞
 * @author: WangYiLong
 * @create: 2020-08-13 17:40
 **/

@Slf4j
@RestController
@RequestMapping("user")
public class FrontCommentController {

    @Resource
    private SubjectCommentService commentService;

    @Resource
    private SubjectService subjectService;

    @Resource
    private RedisTemplate redisTemplate;


/**
     * 添加子评论
     * @param eduComment
     * @return 返回最新评论数据
     */

    @MyLog(value = "添加课程回复信息")
    @PostMapping("comment/addSon")
    public Result addSonComment(EduComment eduComment){

        Date addTime = new Date();
        eduComment.setAddtime(addTime);
        eduComment.setType(2);
        int count = commentService.insert(eduComment);
        if(count>0){
            List<EduCommentDto> list = subjectService.getCommentsByCourseId(eduComment.getOtherId());
            return new Result(200,"成功",list);
        }else{
            return new Result(601,"添加子评论失败");
        }
    }


/**
     * 添加父评论
     * @param eduComment
     * @return
     */

    @MyLog(value = "添加课程评论信息")
    @PostMapping("comment/addParent")
    public Result addParentComment(EduComment eduComment){
        log.info("******添加父评论");
        Date addTime = new Date();
        eduComment.setAddtime(addTime);
        eduComment.setpCommentId(0);
        eduComment.setType(2);
        int count = commentService.insertParent(eduComment);
        if(count>0){
            log.info("******添加父评论"+eduComment.toString());
            List<EduCommentDto> list = subjectService.getCommentsByCourseId(eduComment.getOtherId());
            return new Result(200,"成功",list);
        }else{
            return new Result(602,"添加父评论失败");
        }
    }


/**
     * 点赞
     * @param commentId
     * @return
     */

    @MyLog(value = "课程评论点赞")
    @PostMapping("comment/addPraise/{commentId}")
    public Result addPraise(@PathVariable("commentId") Integer commentId){

       List<EduCommentDto> list = commentService.addPraise(commentId);
        if(CollectionUtils.isEmpty(list)){
            return new Result(603,"修改失败");
        }
        return new Result(200,"成功",list);
    }
}

